package com.xhj.xmly.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.xhj.xmly.R;
import com.xhj.xmly.bean.WelcomeData;
import com.xhj.xmly.callback.HttpService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class WelcomeActivity extends AppCompatActivity implements Callback<WelcomeData> {

    private ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        image = (ImageView) findViewById(R.id.welcome_iv);
        init();

    }
    private void init() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://adse.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        HttpService service = retrofit.create(HttpService.class);
        service.getWelcome().enqueue(this);
    }

    @Override
    public void onResponse(Call<WelcomeData> call, Response<WelcomeData> response) {
        String cover = response.body().getData().get(0).getCover();
        Picasso.with(this).load(cover).into(image);
        new SleepThread().start();
    }

    @Override
    public void onFailure(Call<WelcomeData> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(this, "网络异常", Toast.LENGTH_SHORT).show();
    }


    private class SleepThread extends Thread {
        @Override
        public void run() {
            super.run();
            try {
                Thread.sleep(2 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }

    }
}

